<?php
class Visi_View_Helper_GoogleChart extends Zend_View_Helper_Abstract {

    const API_URI = 'http://chart.apis.google.com/chart?';
    
    protected $_params = array(
        'cht' => null, // type
        'chs' => array(
            'width' => null,
            'height' => null,
        ), // size
        'chd' => array(), // data
    );
    
    public function __construct() {
    
        return $this;
    
    }
    
    public function setParams(Array $params) {
    
        $this->_params = $params;
    
    }
    
    public function setParam($name, $value) {
    
        $this->_params[$name] = $value;
    
    }
    
    public function getParams() {
    
        return $this->_params;
    
    }
    
    public function getParam($param) {
    
        if (!array_key_exists($param, $this->_params)) {
            throw new Visi_View_Helper_GoogleChart_Exception('You have requested a parameter that is not available.');
        }
    
    }
    
    public function getQueryString() {
    
        $params = $this->getParams();
        $params['chs'] = $this->getSize();
        $params['chd'] = $this->getData();
        return implode('&', $params);
    
    }
    
    public function setType($type) {
    
        $this->setParam('cht', $type);
        return $this;
    
    }
    
    public function setSize($width, $height) {
    
        $this->setParam('chs', "{$width}x{$height}");
        return $this;
    
    }
    
    public function getSize() {
    
        return "{$this->_params['chs']['width']}x{$this->_params['chs']['height']}";
    
    }
    
    public function setData($data) {
    
        $this->setParam('chd', $data);
        return $this;
    
    }
    
    public function getData($type = 'string') {
    
        // @todo Finish this
    
    }
    
    public function googleChart() {
    
        if (is_null($this->_params['cht'])) {
            throw new Visi_View_Helper_GoogleChart_Exception('You need to specify a chart type.');
        }
        if (empty($this->_params['chd'])) {
            throw new Visi_View_Helper_GoogleChart_Exception('You need to provide some data.');
        }
        if (is_null($this->_params['chs']['width']) || is_null($this->_params['chs']['height'])) {
            throw new Visi_View_Helper_GoogleChart_Exception('You need to specify a width and height.');
        }
        $uri = self::API_URI;
        $queryString = implode('&', $this->getParams());
        return $uri . $queryString;

    }

}